//
//  HYOUDownScrollViewCell.m
//  BestGift
//
//  Created by 温晓宇 on 16/1/5.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "HYOUDownScrollViewCell.h"
#import "HYOUDownScrollView.h"

@interface HYOUDownScrollViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, retain) HYOUBaseCollectionView *downCollectionView;
@property(nonatomic, retain) NSMutableArray *allArray;

@end
@implementation HYOUDownScrollViewCell
//-(void)dealloc{
//    [_myDelegate release];
//    [_downCollectionView release];
//    [_allArray release];
//    [super dealloc];
//}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    self.allArray = [NSMutableArray array];
    NSString *url = @"http://api.liwushuo.com/v2/secondary_banners?gender=2&generation=1";
    [AppTools finalAFNWithURLString:url parameters:nil successAFNBlock:^(id resuposeObject) {
        NSMutableDictionary *bigDic = resuposeObject;
        NSMutableDictionary *dic = [bigDic objectForKey:@"data"];
        NSMutableDictionary *data = [dic objectForKey:@"secondary_banners"];
        for (NSMutableDictionary *dic in data) {
            HYOUHeadScroll *headModel = [[HYOUHeadScroll alloc]init];
            [headModel setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:headModel];
//            [headModel release];
        }
        [self.downCollectionView reloadData];
    }];
    return self;
}
-(void)createSubViews{
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
    flowlayout.itemSize = CGSizeMake(80, 100);
    flowlayout.sectionInset = UIEdgeInsetsMake(20, 5, 5, 10);
    flowlayout.minimumInteritemSpacing = 10;
    flowlayout.minimumLineSpacing = 10;
    flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    [flowlayout release];
    
    self.downCollectionView = [[HYOUBaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 100) collectionViewLayout:flowlayout];
//    self.downCollectionView.backgroundColor = [UIColor whiteColor];
    self.downCollectionView.delegate = self;
    self.downCollectionView.dataSource = self;
    [self addSubview:self.downCollectionView];
//    [_downCollectionView release];
    
    [self.downCollectionView registerClass:[HYOUDownScrollView class] forCellWithReuseIdentifier:@"cell"];

}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.allArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HYOUDownScrollView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    HYOUHeadScroll *model = [self.allArray objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    HYOUHeadScroll *model = [self.allArray objectAtIndex:indexPath.row];
    NSLog(@"%ld", model.target_url.length);
    //截图target_url的前一部分 用作判断
    NSRange range = NSMakeRange(12, 13);
    NSString *name = [model.target_url substringWithRange:range];
    NSLog(@"%@",name);
    //判断是url 还是post 还是topic
    if ([name isEqualToString:@"page?type=url"]) {
        NSRange range = NSMakeRange(30, model.target_url.length - 30);
        NSString *url = [model.target_url substringWithRange:range];
//        url = [url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        url = [url stringByRemovingPercentEncoding]; //ios9
        NSLog(@"%@",url);
        [self.myDelegate showDownWeb:url];
    }
    if ([name isEqualToString:@"page?type=pos"]) {
        NSRange range = NSMakeRange(35, 7);
        NSString *url = [model.target_url substringWithRange:range];
        NSString *url_string = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/posts/%@",url];
        [AppTools finalAFNWithURLString:url_string parameters:nil successAFNBlock:^(id resuposeObject) {
            NSMutableDictionary *bigDic = resuposeObject;
            NSMutableDictionary *data = [bigDic objectForKey:@"data"];
            HYOUOneCellModel *model = [[HYOUOneCellModel alloc]init];
            [model setValuesForKeysWithDictionary:data];
            NSLog(@"%@",model.content_url);
            [self.myDelegate showDownWeb:model.content_url];
        }];
    }
    if ([name isEqualToString:@"page?type=top"]) {
        if (model.target_url.length == 39) {
            NSRange range = NSMakeRange(37, 2);
            NSString *url = [model.target_url substringWithRange:range];
            NSLog(@"%@",url);
            NSString *url_string = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/collections/%@/posts?gender=1&generation=2&limit=20&offset=0",url];
            [self.myDelegate showDownTopic:url_string];
        }
        if (model.target_url.length == 40) {
            NSRange range = NSMakeRange(37, 3);
            NSString *url = [model.target_url substringWithRange:range];
            NSLog(@"%@",url);
            NSString *url_string = [NSString stringWithFormat:@"http://api.liwushuo.com/v2/collections/%@/posts?gender=1&generation=2&limit=20&offset=0",url];
            [self.myDelegate showDownTopic:url_string];
        }
    }
//    NSRange abc = NSMakeRange(1, 3);
//    strA = [strTwo substringWithRange:abc];
//    NSLog(@"%@", strA);
    
    
//    NSLog(@"%ld",indexPath.row);

}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
